package leeon.mobile.autoip;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

public class IPUtil {
	
	//存储ip地址的xml标志定义
	public static final String APP_SSID_LIST= "ssid_list";
	
	public static final String APP_STATIC_IP_IP= ".ip";
	
	public static final String APP_STATIC_IP_MASK= ".mask";
	
	public static final String APP_STATIC_IP_GW= ".gateway";
	
	public static final String APP_STATIC_IP_DNS1= ".dns1";

	public static final String APP_STATIC_IP_DNS2= ".dns2";
	
	public static String[] getSsidList(Context context) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String s = prefs.getString(APP_SSID_LIST, null);
		return (s == null)?null:s.split(";");
	}
	
	public static boolean isStaticSsid(Context context, String ssid) {
		if (ssid == null) return false;
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		String s = prefs.getString(APP_SSID_LIST, "");
		return (s.indexOf(ssid) != -1);
	}
	
	public static IpSetting getIpSettingBySsid(Context context, String ssid) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		if (isStaticSsid(context, ssid)) {
			IpSetting s = new IPUtil().new IpSetting(ssid,
				prefs.getString(ssid+APP_STATIC_IP_IP, null),
				prefs.getString(ssid+APP_STATIC_IP_MASK, null),
				prefs.getString(ssid+APP_STATIC_IP_GW, null),
				prefs.getString(ssid+APP_STATIC_IP_DNS1, null),
				prefs.getString(ssid+APP_STATIC_IP_DNS2, null)
			);
			return s;
		} else {
			return null;
		}
	}
	
	public static void saveOrUpdateIpSettingBySsid(Context context, IpSetting setting) {
		if (setting == null) return;
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		if (isStaticSsid(context, setting.ssid)) {
			prefs.edit()
				.putString(setting.ssid+APP_STATIC_IP_IP, setting.ip)
				.putString(setting.ssid+APP_STATIC_IP_MASK, setting.mask)
				.putString(setting.ssid+APP_STATIC_IP_GW, setting.gateway)
				.putString(setting.ssid+APP_STATIC_IP_DNS1, setting.dns1)
				.putString(setting.ssid+APP_STATIC_IP_DNS2, setting.dns2)
				.commit();
		} else {
			String s = prefs.getString(APP_SSID_LIST, null);
			s = (s == null)?setting.ssid:s+";"+setting.ssid;
			prefs.edit()
				.putString(APP_SSID_LIST, s)
				.putString(setting.ssid+APP_STATIC_IP_IP, setting.ip)
				.putString(setting.ssid+APP_STATIC_IP_MASK, setting.mask)
				.putString(setting.ssid+APP_STATIC_IP_GW, setting.gateway)
				.putString(setting.ssid+APP_STATIC_IP_DNS1, setting.dns1)
				.putString(setting.ssid+APP_STATIC_IP_DNS2, setting.dns2)
				.commit();
		}
	}
	
	public static void removeIpSettingBySsid(Context context, String ssid) {
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		if (isStaticSsid(context, ssid)) {
			String s = prefs.getString(APP_SSID_LIST, "");
			int start = s.indexOf(ssid);
			int end = start + ssid.length();
			if (start == 0 && end == s.length()) {
				s = null;
			} else if (start == 0) {
				s = s.substring(end+1);
			} else if (end == s.length()) {
				s = s.substring(0, start-1);
			} else {
				s = s.substring(0, start-1) + s.substring(end);
			}
			
			if (s == null) {
				prefs.edit()
					.remove(APP_SSID_LIST)
					.remove(ssid+APP_STATIC_IP_IP)
					.remove(ssid+APP_STATIC_IP_MASK)
					.remove(ssid+APP_STATIC_IP_GW)
					.remove(ssid+APP_STATIC_IP_DNS1)
					.remove(ssid+APP_STATIC_IP_DNS2)
					.commit();
			} else {
				prefs.edit()
					.putString(APP_SSID_LIST, s)
					.remove(ssid+APP_STATIC_IP_IP)
					.remove(ssid+APP_STATIC_IP_MASK)
					.remove(ssid+APP_STATIC_IP_GW)
					.remove(ssid+APP_STATIC_IP_DNS1)
					.remove(ssid+APP_STATIC_IP_DNS2)
					.commit();
			}
		}
	}
	
	class IpSetting {	
		String ssid;
		String ip;
		String mask;
		String gateway;
		String dns1;
		String dns2;
		
		IpSetting(String ssid, String ip, String mask, String gateway, String dns1, String dns2) {
			this.ssid = ssid;
			this.ip = ip;
			this.mask = mask;
			this.gateway = gateway;
			this.dns1 = dns1;
			this.dns2 = dns2;
		}
		
		public String toString() {
			StringBuffer sb = new StringBuffer();
			sb.append("SSID:").append(ssid).append("\n")
				.append("IP:").append(ip).append("\n")
				.append("MASK:").append(mask).append("\n")
				.append("GW:").append(gateway).append("\n")
				.append("DNS1:").append(dns1).append("\n")
				.append("DNS2:").append(dns2).append("\n");
			return sb.toString();
		}
	}
}
